/*
 * Copyright (C) 2021 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.squareup.wire.reflector

import assertk.assertThat
import assertk.assertions.isEqualTo
import com.google.protobuf.DescriptorProtos.FileDescriptorProto
import com.squareup.wire.schema.Location
import com.squareup.wire.schema.Schema
import com.squareup.wire.schema.SchemaLoader
import com.squareup.wire.testing.UnwantedValueStripper
import grpc.reflection.v1alpha.ServerReflectionRequest
import grpc.reflection.v1alpha.ServerReflectionResponse
import okio.ByteString.Companion.decodeBase64
import okio.FileSystem
import org.junit.Test

// Reference golang reflection tests https://github.com/juliaogris/reflect
internal class GrpcurlProto2InteropTest {
  @Test
  fun `list services`() {
    val schema = loadSchema()
    // See golden test file https://github.com/juliaogris/reflect/blob/v0.0.7/testdata/proto2-base64/TestServicesCmd.base64
    val respBase64 = "Cgtsb2NhbGhvc3Q6MBIPCgtsb2NhbGhvc3Q6MDoAMjoKDAoKZWNobzIuRWNobwoqCihncnBjLnJlZmxlY3Rpb24udjFhbHBoYS5TZXJ2ZXJSZWZsZWN0aW9u=="
    val expectedResponse = ServerReflectionResponse.ADAPTER.decode(respBase64.decodeBase64()!!)
    assertThat(
      SchemaReflector(schema, includeDependencies = false).process(
        ServerReflectionRequest(
          list_services = "",
          host = "localhost:0",
        ),
      ),
    ).isEqualTo(expectedResponse)
  }

  val ECHO2_FILEDESCRIPTOR_RESPONSE = "Cg5sb2NhbGhvc3Q6OTA5MBIcCg5sb2NhbGhvc3Q6OTA5MCIKZWNobzIuRWNobyLxTwr5CgoRZWNobzIvZWNobzIucHJvdG8SBWVjaG8yGhxnb29nbGUvYXBpL2Fub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NhdGlvblIIbG9jYXRpb24azgEKCExvY2F0aW9uEhYKBHBhdGgYASADKAVCAhABUgRwYXRoEhYKBHNwYW4YAiADKAVCAhABUgRzcGFuEikKEGxlYWRpbmdfY29tbWVudHMYAyABKAlSD2xlYWRpbmdDb21tZW50cxIrChF0cmFpbGluZ19jb21tZW50cxgEIAEoCVIQdHJhaWxpbmdDb21tZW50cxI6ChlsZWFkaW5nX2RldGFjaGVkX2NvbW1lbnRzGAYgAygJUhdsZWFkaW5nRGV0YWNoZWRDb21tZW50cyLRAQoRR2VuZXJhdGVkQ29kZUluZm8STQoKYW5ub3RhdGlvbhgBIAMoCzItLmdvb2dsZS5wcm90b2J1Zi5HZW5lcmF0ZWRDb2RlSW5mby5Bbm5vdGF0aW9uUgphbm5vdGF0aW9uGm0KCkFubm90YXRpb24SFgoEcGF0aBgBIAMoBUICEAFSBHBhdGgSHwoLc291cmNlX2ZpbGUYAiABKAlSCnNvdXJjZUZpbGUSFAoFYmVnaW4YAyABKAVSBWJlZ2luEhAKA2VuZBgEIAEoBVIDZW5kQn4KE2NvbS5nb29nbGUucHJvdG9idWZCEERlc2NyaXB0b3JQcm90b3NIAVotZ29vZ2xlLmdvbGFuZy5vcmcvcHJvdG9idWYvdHlwZXMvZGVzY3JpcHRvcnBi+AEBogIDR1BCqgIaR29vZ2xlLlByb3RvYnVmLlJlZmxlY3Rpb24="

  @Test
  fun `file_containing_symbol service`() {
    val schema = loadSchema()
    val expectedResponse = ServerReflectionResponse.ADAPTER.decode(ECHO2_FILEDESCRIPTOR_RESPONSE.decodeBase64()!!)
    val response = SchemaReflector(schema, includeDependencies = false).process(
      ServerReflectionRequest(
        file_containing_symbol = "echo2.Echo",
        host = "localhost:0",
      ),
    )
    val unwantedValueStripper = UnwantedValueStripper(clearJsonName = true)
    assertThat(unwantedValueStripper.stripOptionsAndDefaults(response.fileDescriptors[0]))
      .isEqualTo(unwantedValueStripper.stripOptionsAndDefaults(expectedResponse.fileDescriptors[0]))
  }

  @Test
  fun `file_containing_symbol message`() {
    val schema = loadSchema()
    val expectedResponse = ServerReflectionResponse.ADAPTER.decode(ECHO2_FILEDESCRIPTOR_RESPONSE.decodeBase64()!!)
    val response = SchemaReflector(schema, includeDependencies = false).process(
      ServerReflectionRequest(
        file_containing_symbol = "echo2.HelloRequest",
        host = "localhost:0",
      ),
    )
    val unwantedValueStripper = UnwantedValueStripper(clearJsonName = true)
    assertThat(unwantedValueStripper.stripOptionsAndDefaults(response.fileDescriptors[0]))
      .isEqualTo(unwantedValueStripper.stripOptionsAndDefaults(expectedResponse.fileDescriptors[0]))
  }

  @Test
  fun `file_containing_symbol rpc`() {
    val schema = loadSchema()
    val expectedResponse = ServerReflectionResponse.ADAPTER.decode(ECHO2_FILEDESCRIPTOR_RESPONSE.decodeBase64()!!)
    val response = SchemaReflector(schema, includeDependencies = false).process(
      ServerReflectionRequest(
        file_containing_symbol = "echo2.Echo.HelloStream",
        host = "localhost:0",
      ),
    )
    val unwantedValueStripper = UnwantedValueStripper(clearJsonName = true)
    assertThat(unwantedValueStripper.stripOptionsAndDefaults(response.fileDescriptors[0]))
      .isEqualTo(unwantedValueStripper.stripOptionsAndDefaults(expectedResponse.fileDescriptors[0]))
  }

  @Test
  fun `file_by_filename`() {
    val schema = loadSchema()
    val expectedResponse = ServerReflectionResponse.ADAPTER.decode(ECHO2_FILEDESCRIPTOR_RESPONSE.decodeBase64()!!)
    val response = SchemaReflector(schema, includeDependencies = false).process(
      ServerReflectionRequest(
        file_by_filename = "echo2/echo2.proto",
        host = "localhost:0",
      ),
    )
    val unwantedValueStripper = UnwantedValueStripper(clearJsonName = true)
    assertThat(unwantedValueStripper.stripOptionsAndDefaults(response.fileDescriptors[0]))
      .isEqualTo(unwantedValueStripper.stripOptionsAndDefaults(expectedResponse.fileDescriptors[0]))
  }

  private val ServerReflectionResponse.fileDescriptors
    get() = file_descriptor_response!!.file_descriptor_proto.map {
      FileDescriptorProto.parseFrom(
        it.toByteArray(),
      )
    }
}

private fun loadSchema(): Schema {
  return SchemaLoader(FileSystem.SYSTEM)
    .apply {
      initRoots(
        // TODO(juliaogris): Can we derive dependencies transitively?
        sourcePath = listOf(
          Location.get("src/main/resources", "grpc/reflection/v1alpha/reflection.proto"),
          Location.get("src/test/proto", "echo2/echo2.proto"),
          Location.get("src/test/proto", "google/api/annotations.proto"),
          Location.get("src/test/proto", "google/api/http.proto"),
          Location.get("src/test/proto", "google/protobuf/any.proto"),
        ),
        protoPath = listOf(),
      )
    }
    .loadSchema()
}
